package com.xxx.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.demo.entity.CustomerEntity;
import com.xxx.demo.entity.TUsers;
import com.xxx.demo.service.CustomerService;
import com.xxx.demo.mapper.CustomerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity>
    implements CustomerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerServiceImpl.class);
    @Autowired(required = false)
    private CustomerMapper customerMapper;

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public CustomerEntity findByCustomerId(Long id){
        CustomerEntity customerEntity = customerMapper.selectById(id);
        if (ObjectUtil.isEmpty(customerEntity)){
            throw new RuntimeException("数据不存在！");
        }
        return customerEntity;
    }

    /**
     * 根据账号密码查询用户
     * @param customerName
     * @param customerPwd
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public CustomerEntity findByCustomerNameAndPwd(String customerName,String customerPwd){
        LOGGER.info("根据账号密码查询用户入参：customerName = " + customerName  + "  customerPwd = " + customerPwd);
        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",customerName).eq("customer_pwd",customerPwd).eq("delete_flag",0);
        return customerMapper.selectOne(queryWrapper);
    }

    /**
     * 注册用户
     * @param customerEntity
     * @return
     */
    @Override
    @Transactional
    public Integer registerCustomer(CustomerEntity customerEntity){
        if(ObjectUtil.isEmpty(customerEntity)){
            throw new RuntimeException("请先填入数据！");
        }
        customerEntity.setCreationDate(new Date());
        return customerMapper.insert(customerEntity);
    }

    /**
     *根据账号查询
     * @param customerName
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Integer queryCustomerByUserName(String customerName){
        //根据用户名密码手机号查询用户
        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_name",customerName).eq("delete_flag",0);
        //queryWrapper.eq("customer_pwd",customerEntity.getCustomerPwd());
        //queryWrapper.eq("customer_phone",customerEntity.getCustomerPhone());
        CustomerEntity customerEntity1 = customerMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(customerEntity1)){
            return 0;
        }else {
           return  1;
        }
    }

    /**
     * 根据id修改
     * @param customer
     * @return
     */
    @Override
    @Transactional
    public Integer updateByCustomerId(CustomerEntity customer){
        CustomerEntity customerEntity = customerMapper.selectById(customer.getId());
        if (ObjectUtil.isEmpty(customerEntity)){
            return 0;
        }
        return customerMapper.updateById(customer);
    }

    /**
     * 通过分页查询用户
     * @param pageNo
     * @param pageSize
     * @param customer
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public IPage<CustomerEntity> selectCustomerPage(Integer pageNo, Integer pageSize, CustomerEntity customer){
        Page<CustomerEntity> page=new Page<>(pageNo,pageSize);
        return customerMapper.selectCustomer(page,customer);
    }

    /**
     * 根据id删除（假）
     * @param id
     */
    @Override
    @Transactional
    public Integer deleteByCustomer(Long id){
        CustomerEntity customerEntity = this.findByCustomerId(id);
        if (ObjectUtil.isEmpty(customerEntity)){
            return 0 ;
        }
        customerEntity.setDeleteFlag(1);
        int i = customerMapper.updateById(customerEntity);
        LOGGER.info("根据id假删除出参：{}" + customerEntity);
        return i;
    }

    public CustomerEntity findById(String id){
        return customerMapper.findById(id);
    }
}




